package mobi.zeedev.flyingsaucer.common.CredentialsModule.impl;

import android.content.Context;
import android.content.SharedPreferences;
import com.google.inject.Inject;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HomeInfo extends HashMap<String, String> implements Serializable {

//	@Index(value=0)
//	@Meta (Regex="(.*)", XPath="//td[text() = \"Your E-Mail: \"]/../td[last()]")
//	public static final String E_MAIL  = "Your E-Mail: ";
//	
//	@Index(value=1)
//	@Meta(Regex = "You've tasted (\\d*?) of \\d*? Brews", XPath = "//h4") 
//	public static final String PLATE_CURRENT_TOTAL = "You've tasted ";
//	
//	@Index(value=2)
//	@Meta(Regex = "You've tasted \\d*? of (\\d*?) Brews", XPath = "//h4")
//	public static final String PLATE_REQUIRED_TOTAL = " of ";
//	
//	@Index(value=3)
//	@Meta(Regex = "Welcome Back, (.*)", XPath = "//h3")
//	public static final String WELCOME_NAME = "Welcome Back, ";

    public static final String WELCOME_NAME = "WELCOME_NAME";
    public static final String E_MAIL = "E_MAIL";
    public static final String PLATE_CURRENT_TOTAL = "PLATE_CURRENT_TOTAL";
    public static final String PLATE_REQUIRED_TOTAL = "PLATE_REQUIRED_TOTAL";

    public static final String E_MAIL_WEB = "Your E-Mail: ";
    public static final String WELCOME_BACK_NAME_WEB = "Welcome Back, ";
    public static final String PLATE_CURRENT_TOTAL_WEB = "You've tasted ";
    public static final String PLATE_REQUIRED_TOTAL_WEB = " of ";

    public static final String ADDRESS = "Your Address: ";
    public static final String DATE_OF_BIRTH = "Your Date of Birth: ";
    public static final String HOME_SAUCER = "Your Home Saucer: ";
    public static final String MESSAGE_COUNT = "View your messages (";


    public static final Integer COUNT = 8;
    public static final String TAG = "HomeInfo";
    private static final String CACHED_HOME_INFO = "CACHED_HOME_INFO";

    private Date lastQuery = null;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Business Logic
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    public HomeInfo() {
        super();
        //load from prefs

    }

    public synchronized void reset() {
        this.clear();
        lastQuery = null;
    }

    public synchronized Date getLastQuery() {
        return lastQuery;
    }

    public synchronized void setLastQuery(Date lastQuery) {
        this.lastQuery = lastQuery;
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Properties
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public synchronized Boolean isEntityEmpty() {
        Boolean ret = true;
        for (String cur : this.values()) {
            if (cur != null) {
                if (cur.length() > 0) {
                    ret = false;
                    break;
                }
            }
        }
        return ret;
    }

    public synchronized Boolean hasEmail() {
        Boolean ret = false;
        //TODO null checking
        String cur = this.get(E_MAIL);
        ret = (cur != null && cur.length() > 0);
        return ret;
    }

    public synchronized void didRefresh() {
        this.lastQuery = Calendar.getInstance().getTime();
    }

    //TODO this doesn't work
    public synchronized Boolean shouldRefresh() {
        if (lastQuery == null) {
            return true;
        } else if (isTimeElapsed(-15)) {
            return true;
        } else {
            return false;
        }
//		return isTimeElapsed(-15);

    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Utility
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    //TODO this doesn't work  or does it???
    private synchronized Boolean isTimeElapsed(Integer duration) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, duration);
        int res = cal.getTime().compareTo(this.lastQuery);
        if (res > 0) {
            return true;
        } else {
            return false;
        }

    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Preferences
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    public synchronized void bindToPreferences(Context mContext) {
        SharedPreferences preferences = mContext.getSharedPreferences(CACHED_HOME_INFO, Context.MODE_PRIVATE);
        if (!this.isEmpty()) {
            SharedPreferences.Editor e = preferences.edit();
            e.clear();

            for (Map.Entry<String, String> curEntry : this.entrySet()) {
                e.putString(curEntry.getKey(), curEntry.getValue());

            }

            e.commit();
        }

    }

    @Inject
    public synchronized void bindFromPreferences(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(CACHED_HOME_INFO, Context.MODE_PRIVATE);
        if (!prefs.getAll().isEmpty()) {
            this.clear();
            for (Map.Entry<String, ?> curEntry : prefs.getAll().entrySet()) {
                this.put(curEntry.getKey(), curEntry.getValue().toString());
            }
        }
    }


}
